package top.zhangjianyong.tools.bill.manager.resolver;

import cn.hutool.core.collection.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.zhangjianyong.tools.entity.AliBill;
import top.zhangjianyong.tools.entity.WaBill;
import top.zhangjianyong.tools.factory.AliBillFactory;
import top.zhangjianyong.tools.service.mybatis.IAliBillService;
import top.zhangjianyong.tools.service.mybatis.IWaBillService;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Component
public class AliBillFileResolver extends BaseBillFileResolver<AliBill> implements BillFileResolver<AliBill> {

    @Autowired
    private IAliBillService iAliBillService;

    @Autowired
    public AliBillFileResolver(IWaBillService iWaBillService) {
        super(iWaBillService);
    }

    @Override
    public List<String> clean(List<String> lines) {
        List<String> list = new ArrayList<>();
        boolean flag = false;
        for (int i = 0; i < lines.size(); i++) {
            String s = lines.get(i);
            if (s.contains("支付宝（中国）网络技术有限公司  电子客户回单")) {
                flag = true;
                i++;//跳过一行
            } else if (flag) {
                list.add(s);
            }
        }
        return list;
    }

    @Override
    public void saveBill(AliBill aliBill) {
        iAliBillService.save(aliBill);
    }

    @Override
    public void updateBill(AliBill aliBill) {
        iAliBillService.updateById(aliBill);
    }

    @Override
    public AliBill qryByOrderNo(String orderNo, Long fileId) {
        return iAliBillService.lambdaQuery()
                .eq(AliBill::getOrderNo, orderNo)
                .eq(AliBill::getFileId, fileId)
                .one();
    }

    @Override
    public List<AliBill> createBills(List<String> lines, Long fileId) {
        return lines.stream()
                .map(line -> AliBillFactory.create(line, fileId))
                .collect(Collectors.toList());
    }

    @Override
    public String getPlatform() {
        return "支付宝";
    }

    @Override
    public boolean canResolver(List<String> lines) {
        if (CollectionUtil.isEmpty(lines))
            return false;
        return lines.stream().anyMatch(line -> line.contains("支付宝（中国）网络技术有限公司  电子客户回单"));
    }

    @Override
    public List<AliBill> getAll() {
        return iAliBillService.listTheLast();
    }

    @Override
    public void afterReadExcel(Long billFileId) {

        //退款单原始订单关闭
        List<WaBill> refundList = iWaBillService.lambdaQuery()
                .eq(WaBill::getFileId, billFileId)
                .eq(WaBill::getState, "关闭")
                .isNotNull(WaBill::getOriginOrderNo).list();
        refundList.stream().map(waBill -> waBill.getOriginOrderNo())
                .forEach(originOrderNo -> {
                    iWaBillService.updateStateByOrderNo(originOrderNo, "关闭");
                });

    }
}
